package com.jtbd.service.campus;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jtbd.constants.StatusConstant;
import com.jtbd.domain.Campus;
import com.jtbd.dto.http.CampusHttpDto;
import com.jtbd.mapper.CampusMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class CampusService {
    @Resource
    private CampusMapper campusMapper;
    @Resource
    private ObjectMapper objectMapper;

    /**
     * 添加校区信息
     *
     * @param campus 校区对象，包含校区的基本信息
     * @return 添加成功返回校区的ID，添加失败返回null
     */
    public Long addCampus(Campus campus) {
        // 执行插入操作
        int result = campusMapper.insert(campus);
        // 判断插入是否成功，成功则返回校区ID，失败返回null
        if (result > 0) {
            return campus.getId();
        }
        return null;
    }

    /**
     * 封禁校区
     *
     * @param campusId 校区ID
     * @return 成功返回true，添加失败返回false
     */
    public boolean banCampus(Long campusId) {
        LambdaUpdateChainWrapper<Campus> wrapper = new LambdaUpdateChainWrapper<>(campusMapper);
        wrapper.eq(Campus::getId, campusId);
        wrapper.set(Campus::getStatus, StatusConstant.BAN);
        return campusMapper.update(wrapper) > 0;
    }

    public Long addCampus(CampusHttpDto campus) {
        Campus campusDb = objectMapper.convertValue(campus, Campus.class);
        return addCampus(campusDb);
    }
}
